package com.qfedu.mall.service.Impl;

import com.qfedu.mall.beans.entity.UserAddr;
import com.qfedu.mall.common.vo.AdrrId;
import com.qfedu.mall.common.vo.ResStatus;
import com.qfedu.mall.common.vo.ResultVO;
import com.qfedu.mall.beans.vo.UserAddrVO;
import com.qfedu.mall.mapper.UserAddrMapper;
import com.qfedu.mall.service.UserAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserAddrServiceImpl implements UserAddrService {
    @Autowired
    UserAddrMapper userAddrMapper;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO listAddrsByUid(Integer userId) {
        final ResultVO resultVO = new ResultVO();

            UserAddr userAddrs = userAddrMapper.selectAddrsByUid(userId,1);
            System.out.println(userAddrs);
            resultVO.setData(userAddrs);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");



        return resultVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO insertByAid( UserAddrVO userAddrVO) {
        System.out.println(userAddrVO);
        final ResultVO resultVO = new ResultVO();

        int a = userAddrMapper.insertUserAddr(userAddrVO);

        if(a>0){
            List<UserAddrVO> users = userAddrMapper.getAddressByid(userAddrVO.getUserId());
            resultVO.setData(users);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<UserAddrVO> users = userAddrMapper.getAddressByid(userAddrVO.getUserId());
            resultVO.setData(users);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("插入失败");
            return resultVO;
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO deleteByAid(AdrrId adrrId) {
        final ResultVO resultVO = new ResultVO();

        int a = userAddrMapper.deleteUserAddrByAid(adrrId.getAddrId());

        if(a>0){
            List<UserAddrVO> users = userAddrMapper.getAddressByid(adrrId.getUserId());
            resultVO.setData(users);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<UserAddrVO> users = userAddrMapper.getAddressByid(adrrId.getUserId());
            resultVO.setData(users);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("删除失败");
            return resultVO;}

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO updateByAid( UserAddrVO userAddrVO) {
        final ResultVO resultVO = new ResultVO();

        int a = userAddrMapper.updateUserAddr(userAddrVO);
        System.out.println("修改结果：" + a);
        if(a>0){
            List<UserAddrVO> users = userAddrMapper.getAddressByid(userAddrVO.getUserId());
            resultVO.setData(users);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            return resultVO;
        }else {
            List<UserAddrVO> users = userAddrMapper.getAddressByid(userAddrVO.getUserId());
            resultVO.setData(users);
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("修改失败");
            return resultVO;}
    }

    @Override
    public ResultVO getAlladdr(Integer userId) {

        List<UserAddrVO> address = userAddrMapper.getAddressByid(userId);
        System.out.println("------------------");
        address.forEach(System.out::println);
        System.out.println("------------------");
        return new ResultVO(ResStatus.OK,"success",address);
    }
}
